# Dih4.py - A python module for exploring Dih4 the symmetric group over a square.

"""Represents elements of the symmetric group of Dih4."""
class Dih4:
    """Creates an element of Dih4"""
    def __init__(self, perm):
        self.__perm = perm

    """Returns a rotation of this element 90 degrees clockwise."""
    def rotate(self):
        perm = self.__perm[1:4]
        perm.append(self.__perm[0])
        return Dih4(perm)

    """Flip this element about the X-axis."""
    def flipX(self):
        perm = [self.__perm[3], self.__perm[2], self.__perm[1], self.__perm[0]]
        return Dih4(perm)

    """Flip this element about the Y-axis."""
    def flipY(self):
        perm = [self.__perm[1], self.__perm[0], self.__perm[3], self.__perm[2]]
        return Dih4(perm)

    """Multiply this element with another."""
    def __mul__(self, b):
        p1 = b.__perm
        p2 = self.__perm
        return Dih4([p1[p2[0]-1], p1[p2[1]-1], p1[p2[2]-1], p1[p2[3]-1]])

    """Return the string representation of this element."""
    def __repr__(self):
        return self.__str__()
        
    """Return the string representation of this element."""
    def __str__(self):
        cstr = "";
        if self.__perm == [1, 2, 3, 4]:
            cstr = "Identity\n(1)\n"
        if self.__perm == [2, 3, 4, 1]:
            cstr = "R_90\n(1 2 3 4)\n"
        if self.__perm == [3, 4, 1, 2]:
            cstr = "R_180\n(1 3)(2 4)\n"
        if self.__perm == [4, 1, 2, 3]:
            cstr = "R_270\n(1 4 3 2)\n"
        if self.__perm == [4, 3, 2, 1]:
            cstr = "Flip_X\n(1 4)(2 3)\n"
        if self.__perm == [2, 1, 4, 3]:
            cstr = "Flip_Y\n(1 2)(3 4)\n"
        if self.__perm == [1, 4, 3, 2]:
            cstr = "Diagonal_1\n(2 4)\n"
        if self.__perm == [3, 2, 1, 4]:
            cstr = "Diagonal_2\n(1 3)\n"
        p = self.__perm
        return cstr + ("%d\t%d\n\n\n%d\t%d" % (p.index(1)+1, p.index(2)+1, p.index(4)+1, p.index(3)+1))

one = Dih4([1, 2, 3, 4])   
# (1 2 3 4) = (1) ----------- Identity
# (1 2 3 4)

two = Dih4([2, 3, 4, 1])   
# (1 2 3 4) = (1 2 3 4) ----- R_90
# (2 3 4 1)

three = Dih4([3, 4, 1, 2])   
# (1 2 3 4) = (1 3)(2 4) ---- R_180
# (3 4 1 2)

four = Dih4([4, 1, 2, 3])  
# (1 2 3 4) = (1 4 3 2) ----- R_270
# (4 1 2 3)

five = Dih4([4, 3, 2, 1])  
# (1 2 3 4) = (1 4)(2 3) ---- Flip_X
# (4 3 2 1)

six = Dih4([2, 1, 4, 3])     
# (1 2 3 4) = (1 2)(3 4) ---- Flip_Y
# (2 1 3 4)

seven = Dih4([1, 4, 3, 2]) 
# (1 2 3 4) = (2 4) --------- Diagonal_1
# (1 4 3 2)

eight = Dih4([3, 2, 1, 4]) 
# (1 2 3 4) = (1 3) --------- Diagonal_2
# (3 2 1 4)
